<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller;

use app\common\controller\Admin;
use app\basic\controller\api\Store as StoreApi;
use app\admin\controller\ajax\Library as LibarayAjax;
/**
 * 仓库管理
 * @package app\rescue
 */
class Store  extends Admin

{
    
    protected $api;
    protected $property;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new StoreApi();
		$this->property = new LibarayAjax();
	}
    /**
     * 首页
     * @return Json/View
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$storeList = $this->api->storeList($this->scope);
   	  	return $storeList;
   	  }
   	  $enterpriseList = model('Enterprise')->getSelectList($this->scope);
	  $this->assign('enterpriseList',$enterpriseList);
      return $this->fetch();
 	}
 	

 	/**
 	 * 仓库查询
 	 * @return Json
 	 */
 	public function search() {
 		if(IS_AJAX){
 			$storeList = $this->api->search(input('keyword'),input('enterpriseId'));
 			return $storeList;
 		}
 	}
 	
 	
 	/**
 	 * 添加仓库
 	 * @return Json/View
 	 */
 	public function add(){
 		if(IS_POST){
 			$data = input('post.');
 			return $this->api->add($data);
 		}
 		$enterpriseList = model('Enterprise')->getSelectList($this->scope);
 		$this->assign('enterpriseList',$enterpriseList);
 		return $this->fetch();
 	}
 	
 	/**
 	 * 编辑仓库
 	 * @param string $craftId
 	 * @return Json/View
 	 */
 	public function edit($storeId = null){
 		if(IS_POST){
 			$data = input('post.');
 			return $this->api->edit($data,$storeId);
 		}
 		$enterpriseList = model('Enterprise')->column('fullName','id');
 		$this->assign('enterpriseList',$enterpriseList);
 		$store = model('Store')->find($storeId);
 		$this->assign('info',$store);
 		return $this->fetch();
 		
 	}
 	
 	/**
 	 * 删除仓库
 	 * @return Json
 	 */
 	public function del($storeId = null) {
 		if(IS_AJAX){
 			return $this->api->del($storeId);
 		}
 	}



	/**
	 * 设置仓库位置
	 * @param string $position
	 * @param string $enterprise
	 * @return mixed
	 */
	public function setBuilding($position = null,$storeId = null){
		$center = $this->api->getStorePosition($storeId)->getData();
		$center = $center['code']?$center['data']:null;
		$this->assign('center',$center);
		$this->assign('position',$position);
		return $this->fetch();
	}
}
